package cn.itcast.demo;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ServletContextDemo extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//            1.获取ServletContext对象
        ServletContext context = getServletContext();
//        根据key获取值
        String globalDesc = context.getInitParameter("globalDesc");
        System.out.println(globalDesc);
//        获取虚拟目录
        String path = context.getContextPath();
        System.out.println(path);
//        根据虚拟目录获取绝对路径
        String realPath = context.getRealPath("/");
        System.out.println(realPath);
//        获取文本文件绝对路径
        String a = context.getRealPath("/a.txt");
        System.out.println(a);
        String b = context.getRealPath("/WEB-INF/b.txt");
        System.out.println(b);
        String c = context.getRealPath("/WEB-INF/classes/c.txt");
//        设置共享数据
        System.out.println(c);
        context.setAttribute("username","伍鉴");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
